package pers.wang.toolset.ali.rocketmq.spring.boot.autoconfigure.condition;

import java.util.Map;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import pers.wang.toolset.ali.rocketmq.spring.boot.autoconfigure.annotation.EnableAliRocketMq;

public class EnableConsumerCondition implements Condition {

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// step 1 : 获取启动类上的注解
		Map<String, Object> aaMap = context.getBeanFactory().getBeansWithAnnotation(SpringBootApplication.class);
		// step 2 : 获取或有类上具有EnableHttpClient注解的类
		Map<String, Object> haMap = context.getBeanFactory().getBeansWithAnnotation(EnableAliRocketMq.class);
		
		if (!aaMap.isEmpty() && !haMap.isEmpty()) {
			for (Map.Entry<String, Object> entry : aaMap.entrySet()) {
				if (haMap.containsKey(entry.getKey()) && entry.getValue().equals(haMap.get(entry.getKey()))) {
					return context.getBeanFactory().findAnnotationOnBean(entry.getKey(), EnableAliRocketMq.class)
							.consumer();
				}
			}
		}
		return false;
	}

}
